package com.shugl.study.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Description 给一个数组和目标值，两个和为目标值的下标，数组值不重复。
 *
 * @author Shugl
 * @version 1.0
 * @since 2021/6/15 17:25
 */
public class ListTwoSum {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{7, 11, 2, 5}, 9)));
    }


    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> tem = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer index = tem.get(target - nums[i]);
            if (index != null) {
                return new int[]{index, i};
            } else {
                tem.put(nums[i], i);
            }
        }
        return null;
    }
}
